package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.BaseSaleAttr;
import com.atguigu.gmall.product.model.SpuImage;
import com.atguigu.gmall.product.model.SpuInfo;
import com.atguigu.gmall.product.model.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuManageService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SpuManageController {

    @Autowired
    private SpuManageService spuManageService;

    /**
     * 商品SPU分页
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/{page}/{size}")
    public Result getSpuByPage(@PathVariable("page") long page, @PathVariable("size") long size, @RequestParam("category3Id") Long category3Id) {
        //1.构建分页对象
        Page<SpuInfo> spuInfoPage = new Page<>(page, size);
        //2.调用业务层实现分页
        spuInfoPage = spuManageService.getSpuByPage(spuInfoPage, category3Id);
        return Result.ok(spuInfoPage);
    }

    /**
     * 查询当前平台包含所有销售属性列表
     *
     * @return
     */
    @GetMapping("/baseSaleAttrList")
    public Result getBaseSaleAttrList() {
        List<BaseSaleAttr> list = spuManageService.getBaseSaleAttrList();
        return Result.ok(list);
    }

    @GetMapping("/getSpuInfo/{id}")
    public Result getSpuInfo(@PathVariable Long id) {
        SpuInfo list = spuManageService.getSpuInfo(id);
        return Result.ok(list);
    }

    /**
     * 保存商品SPU信息
     *
     * @param spuInfo
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        spuManageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }


    @PostMapping("/updateSpuInfo")
    public Result updateSpuInfo(@RequestBody SpuInfo spuInfo) {
        spuManageService.updateSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 根据商品spuID查询当前商品所有商品图片
     *
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result getSupuImagheList(@PathVariable Long spuId){
        List<SpuImage> list = spuManageService.getSpuImageList(spuId);
        return Result.ok(list);
    }

    /**
     * 根据spuId查询商品SPU所有销售属性包含属性值列表
     *
     * @param spuId
     * @return
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable("spuId") Long spuId) {
        List<SpuSaleAttr> list = spuManageService.getSpuSaleAttrList(spuId);
        return Result.ok(list);
    }
}
